package com.ruoyi.web.controller.test2;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.domain.SharedFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AssetManagement;
import com.ruoyi.system.service.IAssetManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资产管理Controller
 * 
 * @author 王帝
 * @date 2022-08-03
 */
@RestController
@RequestMapping("/system/management")
public class AssetManagementController extends BaseController
{
    @Autowired
    private IAssetManagementService assetManagementService;

    /**
     * 导入数据管理列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AssetManagement> util = new ExcelUtil<AssetManagement>(AssetManagement.class);
        List<AssetManagement> dataManagementList = util.importExcel(file.getInputStream());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = getUsername();
        String message = assetManagementService.importData(dataManagementList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SharedFile> util = new ExcelUtil<SharedFile>(SharedFile.class);
        return util.importTemplateExcel("案卷数据");
    }


    /**
     * 查询资产管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetManagement assetManagement)
    {
        startPage();
        List<AssetManagement> list = assetManagementService.selectAssetManagementList(assetManagement);
        return getDataTable(list);
    }

    /**
     * 导出资产管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:export')")
    @Log(title = "资产管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetManagement assetManagement)
    {
        List<AssetManagement> list = assetManagementService.selectAssetManagementList(assetManagement);
        ExcelUtil<AssetManagement> util = new ExcelUtil<AssetManagement>(AssetManagement.class);
        util.exportExcel(response, list, "资产管理数据");
    }

    /**
     * 获取资产管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") Integer assetId)
    {
        return AjaxResult.success(assetManagementService.selectAssetManagementByAssetId(assetId));
    }

    /**
     * 新增资产管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:add')")
    @Log(title = "资产管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetManagement assetManagement)
    {
        assetManagement.setUserId(getUserId());
        assetManagement.setDeptId(getDeptId());
        System.out.println("assetManagement = " + assetManagement);
        return toAjax(assetManagementService.insertAssetManagement(assetManagement));
    }

    /**
     * 修改资产管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "资产管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetManagement assetManagement)
    {
        return toAjax(assetManagementService.updateAssetManagement(assetManagement));
    }

    /**
     * 删除资产管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:remove')")
    @Log(title = "资产管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assetIds}")
    public AjaxResult remove(@PathVariable Integer[] assetIds)
    {
        return toAjax(assetManagementService.deleteAssetManagementByAssetIds(assetIds));
    }
}
